package kr.saeildamil.androidforstudy2.ch20.common;

import android.app.Activity;
import kr.saeildamil.androidforstudy2.R;
import android.app.ListFragment;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class WordListFragment extends ListFragment {
	
	String[] words;
	// String[] desc;
	onWordChangedListener mHost;
	
	public interface onWordChangedListener{
		public void onWordChanged(int index);
	}
	
	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		
		try{
			mHost = (onWordChangedListener) activity;
		}catch(ClassCastException e){
			// activity 는 반드시 OnWordChangedListener를 구현해야 한다;
			throw new ClassCastException("Activity must implement OnWordChangedListener.");
		}
	}
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		words = getResources().getStringArray(R.array.words);
		// desc = getResources().getStringArray(R.array.desc);
	}
	
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		ArrayAdapter<CharSequence> adapter = 
				ArrayAdapter.createFromResource(getActivity(), R.array.words, android.R.layout.simple_list_item_1);
		setListAdapter(adapter);
		getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
	}
	
	@Override
	public void onListItemClick(ListView l, View v, int position, long id) {
		super.onListItemClick(l, v, position, id);
		mHost.onWordChanged(position);
	/*	String str = words[position] + " : " + desc[position];
		Toast.makeText(getActivity(), str, Toast.LENGTH_SHORT).show();*/
		
	}
}
